<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/2/17 16:59
 * Remark:
 */

namespace App\Http\Controllers\Contract;

use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Http\Controllers\Controller;
use App\Http\Requests\Contract\AddContractTypePost;
use App\Http\Requests\Contract\ListContractTypeGet;
use App\Service\Contract\ContractTypeService;

class ContractTypeController extends Controller
{
    private ContractTypeService $_contractTypeService;

    public function __construct()
    {
        $this->middleware([
            'contractTypeLeg'
        ]);
        $this->_contractTypeService = new ContractTypeService();
    }

    /**
     * @param AddContractTypePost $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addContractType(AddContractTypePost $request): \Illuminate\Http\JsonResponse
    {
        $data = $request->all();
        $res =  $this->_contractTypeService->addContractType($data);
        return ResponseFunc::returnJson(
            'ok',
            [
                "contract_type_id" =>$res
            ],
            '新增合同类型成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );
    }

    /**
     * 修改合同类型
     * @param int $contractTypeId
     * @return \Illuminate\Http\JsonResponse
     */
    public function editContractType(int $contractTypeId): \Illuminate\Http\JsonResponse
    {
        $data = request()->post();
        $data['contract_type_id'] = $contractTypeId;
        $data['is_allow_type'] = $data['is_allow_type'] ?? 0;
        $this->_contractTypeService->editContractType($data);
        return ResponseFunc::returnJson(
            'ok',
            null,
            '修改合同类型成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );
    }

    /**
     * 删除合同类型
     * @param int $contractTypeId
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteContractType(int $contractTypeId): \Illuminate\Http\JsonResponse
    {
        $this->_contractTypeService->deleteContractType($contractTypeId);
        return ResponseFunc::returnJson(
            'ok',
            null,
            '删除合同类型成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );
    }

    /**
     * 获取合同详情
     * @param int $contractTypeId
     * @return \Illuminate\Http\JsonResponse
     */
    public function getContractTypeInfo(int $contractTypeId): \Illuminate\Http\JsonResponse
    {
        return ResponseFunc::returnJson(
            'ok',
            $this->_contractTypeService->getContractTypeInfo($contractTypeId),
            '获取合同类型详情成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );
    }

    /**
     * 获取合同类型下拉
     * @return \Illuminate\Http\JsonResponse
     */
    public function getContractTypeSelect(): \Illuminate\Http\JsonResponse
    {
        return ResponseFunc::returnJson(
            'ok',
            $this->_contractTypeService->getContractTypeSelect(),
            '获取合同类型下拉成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );
    }

    /**
     * 根据部门ID获取合同类型下拉
     * @param ListContractTypeGet $request
     * @return mixed
     */
    public function getContractTypeList(ListContractTypeGet $request): mixed
    {
        $pageSize = $request->get('page_size');
        $filters = $request->get('filters');
        return ResponseFunc::returnJson(
            'ok',
            $this->_contractTypeService->getContractTypeList($pageSize,$filters),
            '获取合同类型列表成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }



}